<?php

namespace app\admin\utils\controller;

use app\admin\utils\model\UtilLang;
use support\Db;
use support\Request;
use support\Response;
use app\admin\utils\model\UtilLangDetail;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
use app\common\services\DragdoneUpService;

/**
 * 语言包管理 
 */
class UtilLangDetailController extends Crud
{
    
    /**
     * @var UtilLangDetail
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new UtilLangDetail;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(Request $request): Response
    {
        // 将url参数传递给前端
        $lang_id = $request->get('lang_id');
        session()->set('lang_id',$lang_id);
        // \support\View::assign(['params'=>$request->get()]);
        return view('utils/util-lang-detail/index',['lang_id'=>$lang_id]);
    }

    public function select(Request $request): Response
    {
        $lang_id = $request->get('lang_id');
        session()->set('lang_id',$lang_id);
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $where['lang_id'] = 0;
        if($request->get('status')==1){
            // 仅查看无数据的  实际是使用leftjoin 进行的关联查询
            // select `a`.`id` from `util_lang_detail` as `a` where `a`.`lang_id` = 0 and not exists (select 1 from `util_lang_detail` as `b` where `b`.`lang_id` = 2 and b.tid = a.id)
            $results = Db::table('util_lang_detail as a')
                ->select("a.id")
                ->where('a.lang_id',0)
                ->whereNotExists(function($query) use($lang_id){
                    $query->select(Db::raw(1))
                        ->from("util_lang_detail as b")
                        ->where("b.lang_id",$lang_id)
                        ->whereRaw('b.tid = a.id');
                })->get()->toArray();
            if($results){
                // 有数据可查询
                $where = ['id'=>['in',array_column($results,'id')]];
            }else{
                // 返回空数据
                $where = ['id'=>0];
            }
        }
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    public function afterQuery($items)
    {
        $lang_id = session('lang_id');
        foreach ($items as $k=>$item){
            $items[$k]['text2'] = UtilLangDetail::where(['lang_id'=>$lang_id,
                'tid'=>$item['id']])->value('text')??'';
            $items[$k]['lang_id'] = $lang_id;
        }
        return $items;
    }


    public function saveData(Request $request): Response{
        $data = $request->post('addData');
        foreach ($data as $item){
            if(!$item['text']) continue;
            $where = [
                'lang_id'=>$item['lang_id'],
                'tid'=>$item['tid'],
            ];
            $info = UtilLangDetail::where($where)->first();
            if($info->exists){
                $info->text = $item['text'];
                $info->save();
            }else{
                $where['text'] = $item['text'];
                $where['created_at'] = date('Y-m-d H:i:s');
                UtilLangDetail::insertGetId($where);
            }
        }
        // 生成语言包配置
        UtilLang::generate();
        return json(['code'=>0,'msg'=>'保存成功']);
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('utils/util-lang-detail/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
                    // 拖拽排序更新
            if($request->post('dragDone')==1 && DragdoneUpService::dragDoneUpData($request,$this->model)){
                return $this->json(0,'ok');
            }
            return parent::update($request);
        }
        return view('utils/util-lang-detail/update');
    }

}
